﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Tenk.Controls
{
    /// <summary>
    /// Summary description for DataPager
    /// </summary>
    /// 

    public class DataPager : WebControl, IPostBackEventHandler
    {
        private int m_PageNo;        
        private int m_PageSize;
        private int m_RecordCount;        

        private string m_PreviousText;
        private string m_NextText;

        private const string CONTROLCLASSNAME = "DataPager";

        private const string HIDDEN_FIELD_PAGE_NO = "_Page_No";
        private const string HIDDEN_FIELD_PAGE_SIZE = "_Page_Size";
        private const string HIDDEN_FIELD_RECORD_COUNT = "_Record_Count";

        private const string HTML_PREVIIUS_TEXT = "Previous";
        private const string HTML_NEXT_TEXT = "Next";

        private const int DEFAULT_RECORD_COUNT = 0;
        private const int DEFAULT_PAGE_SIZE = 16;
        private const int DEFAULT_PAGE_NO = 1;

        public event EventHandler<PageChangedEventArgs> PageChanged;


        //constructor
        public DataPager()
        {
            m_PageNo = DEFAULT_PAGE_NO;
            m_PageSize = DEFAULT_PAGE_SIZE;
            m_RecordCount = DEFAULT_RECORD_COUNT;
            m_PreviousText = HTML_PREVIIUS_TEXT;
            m_NextText = HTML_NEXT_TEXT;       
        }

        public  int PageNo
        {
            get 
            {
                return m_PageNo;
            }
            set 
            {
                m_PageNo = value; 
            }
        }

        public int PageSize
        {
            get
            {
                return m_PageSize;
            }
            set
            {
               m_PageSize = value;
            }
        }

        public int RecordCount
        {
            get
            {
                return m_RecordCount;
            }
            set
            {
                m_RecordCount = value;
            }
        }

       

        public  string PreviousText
        {
            get { return m_PreviousText; }
            set { m_PreviousText = value; }
        }

        public string NextText
        {
            get { return m_NextText; }
            set { m_NextText = value; }
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            if (Page.IsPostBack)
            {
                HttpRequest request = HttpContext.Current.Request;
                if (!string.IsNullOrEmpty(request[this.UniqueID + HIDDEN_FIELD_PAGE_NO]))
                {
                    m_PageNo = int.Parse(request[this.UniqueID + HIDDEN_FIELD_PAGE_NO]);
                }
                if (!string.IsNullOrEmpty(request[this.UniqueID + HIDDEN_FIELD_PAGE_SIZE]))
                {
                    m_PageSize = int.Parse(request[this.UniqueID + HIDDEN_FIELD_PAGE_SIZE]);
                }
                if (!string.IsNullOrEmpty(request[this.UniqueID + HIDDEN_FIELD_RECORD_COUNT]))
                {
                    m_RecordCount = int.Parse(request[this.UniqueID + HIDDEN_FIELD_RECORD_COUNT]);
                }
            }
        }

        //Page changed event
        protected void OnPageChanged(object sender, PageChangedEventArgs e)
        {
            if (this.PageChanged != null)
            {
                this.PageChanged(sender, e);
            }
        }

        //trigger page click event
        public void RaisePostBackEvent(string eventArgument)
        {            
            if (this.Page.Request["__EVENTTARGET"] == this.UniqueID)
            {
                //get new pager
                //m_PageNo = int.Parse(this.Page.Request["__EVENTARGUMENT"]);
                m_PageNo = int.Parse(eventArgument);
                PageChangedEventArgs e = new PageChangedEventArgs();
                e.CurrentPage = m_PageNo;
                OnPageChanged(this, e);
            }
        }

        protected override void Render(HtmlTextWriter writer)
        {
            if (m_RecordCount > 0)
            {
                base.Render(writer);                
                Page.ClientScript.RegisterHiddenField(this.UniqueID + HIDDEN_FIELD_PAGE_NO, m_PageNo.ToString());
                Page.ClientScript.RegisterHiddenField(this.UniqueID + HIDDEN_FIELD_PAGE_SIZE, m_PageSize.ToString());
                Page.ClientScript.RegisterHiddenField(this.UniqueID + HIDDEN_FIELD_RECORD_COUNT, m_RecordCount.ToString());
            }
        }

        /// <summary>
        /// output pager html contents
        /// </summary>
        /// <param name="writer"></param>
        protected override void RenderContents(HtmlTextWriter writer)
        {
            base.RenderContents(writer);
            ClientScriptManager clientScriptManager = Page.ClientScript;
            int pageCount = (int)Math.Ceiling((double)m_RecordCount/(double)m_PageSize);
            StringBuilder html = new StringBuilder();

            html.AppendFormat("Page {0} of {1} Total Record: {2}&nbsp;&nbsp;&nbsp;", m_PageNo, pageCount, m_RecordCount);
            if (m_PageNo > 1)
            {
                html.AppendFormat("<a href=\"{0}\">{1}</a>&nbsp;&nbsp;", clientScriptManager.GetPostBackClientHyperlink(this, Convert.ToString(m_PageNo - 1)), this.m_PreviousText);
            }
            else
            {
                html.AppendFormat("{0}&nbsp;&nbsp;", m_PreviousText);
            }
            for (int i = 1; i <= pageCount; i++)
            {
                if (i == m_PageNo)
                {
                    html.AppendFormat("&nbsp;{0}", i);
                }
                else
                {
                    html.AppendFormat("&nbsp;<a href=\"{0}\">{1}</a>", clientScriptManager.GetPostBackClientHyperlink(this, i.ToString()), i);
                }
            }
            if (m_PageNo < pageCount)
            {
                html.AppendFormat("&nbsp;&nbsp;&nbsp;<a href=\"{0}\">{1}</a>&nbsp;&nbsp;", clientScriptManager.GetPostBackClientHyperlink(this, Convert.ToString(m_PageNo + 1)), m_NextText);
            }
            else
            {
                html.AppendFormat("&nbsp&nbsp;&nbsp;{0}", m_NextText);
            }
            writer.Write(html.ToString());
        }
    }


    /// <summary>
    /// DataPager event args class
    /// </summary>
    
    
    public class PageChangedEventArgs : EventArgs
    {
        private int m_CurrentPage;

        public int CurrentPage
        {
            get { return m_CurrentPage; }
            set { m_CurrentPage = value; }
        }

        public PageChangedEventArgs()
        {
            m_CurrentPage = 1;
        }

        public PageChangedEventArgs(int currentPage)
        {
            m_CurrentPage = currentPage;
        }
    }
}